package com.jiangyan.hookplus.hook

import android.os.Bundle
import android.view.View
import com.highcapable.yukihookapi.annotation.xposed.InjectYukiHookWithXposed
import com.highcapable.yukihookapi.hook.factory.configs
import com.highcapable.yukihookapi.hook.factory.encase
import com.highcapable.yukihookapi.hook.log.loggerD
import com.highcapable.yukihookapi.hook.log.loggerE
import com.highcapable.yukihookapi.hook.type.java.BooleanClass
import com.highcapable.yukihookapi.hook.type.java.StringClass
import com.highcapable.yukihookapi.hook.xposed.bridge.event.YukiXposedEvent
import com.highcapable.yukihookapi.hook.xposed.proxy.IYukiHookXposedInit
import de.robv.android.xposed.XC_MethodHook
import de.robv.android.xposed.XposedBridge
import de.robv.android.xposed.XposedHelpers

@InjectYukiHookWithXposed
class HookEntry : IYukiHookXposedInit {

    override fun onInit() = configs {
        debugLog {
            tag = "HookPlus"
        }
    }

    //YuKiHook类方法
    override fun onHook() = encase {
        //App名称-麻雀记
        loadApp("com.chrissen.card") {
            //要Hook的类名
            findClass("com.chrissen.component_base.utils.ProUtils", appClassLoader).hook {
                injectMember(tag = "hook麻雀记") {
                    method {
                        //方法名
                        name = "unlockPro"
                        //方法的参数数量
//                        paramCount = 3
                        //方法参数类型xiaohan
//                        param(IntType, DoubleType, FloatType, StringClass, Context::class, Application::class)
                    }
                    //在方法执行前Hook
                    beforeHook {
                        //判断参数值args数组存储方法的参数，下标从0开始，依次是第一个参数，第二个...
//                        if (args[0] == 1 && args[1] == 1.0f) {
//                            //修改返回结果
//                            result = "Hook返回结果"
//                        }
                    }
                    //方法执行后Hook
                    afterHook {
                        //Hook代码
                        result = true
                    }
                }.onHooked { member ->
                    loggerD(msg = "$member has hooked")
                }
            }
        }
        //App名称-MT
        loadApp("bin.mt.plus") {
            //要Hook的类名
            findClass("l.ۖ۟ۖ۬", appClassLoader).hook {
                injectMember {
                    method {
                        //方法名
                        name = "ۛ"
                    }
                    //方法执行后Hook
                    afterHook {
                        //Hook代码
                        result = true
                    }
                }
                injectMember {
                    method {
                        //方法名
                        name = "ۗ"
                    }
                    //方法执行后Hook
                    afterHook {
                        //Hook代码
                        result = true
                    }
                }
                injectMember {
                    method {
                        //方法名
                        name = "۟"
                    }
                    //方法执行后Hook
                    afterHook {
                        //Hook代码
                        result = true
                    }
                }
                injectMember {
                    method {
                        //方法名
                        name = "ۡ"
                    }
                    //在方法执行前Hook
                    beforeHook {}
                    //方法执行后Hook
                    afterHook {
                        //Hook代码
                        result = true
                    }
                }
                injectMember {
                    method {
                        //方法名
                        name = "ۥ"
                    }
                    //方法执行后Hook
                    afterHook {
                        //Hook代码
                        result = true
                    }
                }
                injectMember {
                    method {
                        //方法名
                        name = "ۦ"
                    }
                    //方法执行后Hook
                    afterHook {
                        //Hook代码
                        result = true
                    }
                }

            }
        }
        //App名称-阅读
        loadApp("io.legado.app.release") {
            //要Hook的类名
            findClass("io.legado.app.help.source.m", appClassLoader).hook {
                injectMember {
                    method {
                        //方法名
                        name = "invoke"
                    }
                    //在方法执行前Hook
//                    beforeHook {}
                    //方法执行后Hook
                    afterHook {
                        //Hook代码
                        resultNull()
                    }
                }
            }
        }
        //App名称-TG
        loadApp("org.telegram.messenger") {
            //要Hook的类名
            findClass("org.telegram.messenger.UserConfig", appClassLoader).hook {
                injectMember(tag = "tg") {
                    method {
                        //方法名
                        name = "isPremium"
                        param(BooleanClass)
                    }
                    //在方法执行前Hook
//                    beforeHook {}
                    //方法执行后Hook
                    afterHook {
                        //Hook代码
                        loggerD("HookPluse", "执行了")
                        resultTrue()
                    }
                }
            }
        }
        //App名称-番茄免费小说
        loadApp("com.dragon.read") {
            //hook-vip
            findClass("com.dragon.read.user.h").hook {
                injectMember {
                    method {
                        name = "isVip"
                    }
                    afterHook {
                        XposedBridge.log("执行了$this")
                        resultTrue()
                    }
                }.onHooked { member ->
                    loggerD(msg = "$member Hook 成功")
                }
            }.onPrepareHook {
                loggerD(msg = "番茄 hook start")
            }
            val hookList = listOf(
                "com.dragon.read.reader.ad.readflow.ui.ReadFlowDynamicAdLine",
                "com.bytedance.reader_ad.banner_ad.e.b",
                "com.bytedance.reader_ad.banner_ad.e.c",
                "com.bytedance.reader_ad.banner_ad.e.d",
                "com.dragon.read.ad.banner.ui.e",
                "com.dragon.read.ad.banner.ui.h",
                "com.dragon.read.ad.openingscreenad.brand.ui.a",
                "com.dragon.read.widget.i",
                "com.dragon.read.component.audio.impl.ui.widget.reader.l",
                "com.dragon.read.ad.chapterend.morecardview.a",
                "com.dragon.read.widget.l",
                "com.dragon.read.widget.m",
                "com.dragon.read.widget.n",
                "com.dragon.read.widget.o"
            )
            for (item in hookList) {
                val find = XposedHelpers.findClass(item, this.appClassLoader)
                val declaredMethods = find.getDeclaredMethods()
                for (i in declaredMethods) {
//                    XposedBridge.log("看看${item}类和他的方法${i.name}
                    findClass(item).hook {
                        injectMember {
                            method {
                                name = i.name
                            }
                            beforeHook {
                                XposedBridge.log("执行了$this")
                                resultNull()
                            }
                        }.onHooked { member ->
                            loggerD(msg = "$member Hook 成功")
                        }
                    }.onPrepareHook {
                        loggerD(msg = "番茄去广告 hook start")
                    }
                }
            }

        }
        //App名称-番茄畅听
        loadApp("com.xs.fm") {
            //hook-vip
            findClass("com.xs.fm.mine.impl.MineImpl").hook {

                val listname = listOf(
                    "canShowIMRedDot",
                    "canShowMusicKingKong",
                    "canShowVipRelational",
                    "isVip",
                    "islogin",
                    "isNewUser",
                    "isVipRemind",
                    "hasNoAudioAdPrivilege",
                    "isNoAd",
                    "hasPrivilege"
                )
                for (item in listname) {
                    injectMember {
                        method {
                            name = item
                        }
                        afterHook {
                            resultTrue()
                        }
                    }
                }

            }
            findClass("com.xs.fm.ad.impl.AdImpl").hook {
                injectMember {
                    method {
                        name = "isVipInvertExper"
                    }
                    afterHook {
                        resultTrue()
                    }
                }
            }
            findClass("com.dragon.read.user.g").hook {
                injectMember {
                    method {
                        name = "j"
                    }
                    afterHook {
                        result = true
                    }
                }
                injectMember {
                    method {
                        name = "u"
                    }
                    afterHook {
                        result = 6000
                    }
                }
            }
            findClass("com.xs.fm.mine.impl.MineImpl").hook {
                injectMember {
                    method {
                        name = "getIsUserNeedWeakenVip"
                    }
                    afterHook {
                        resultFalse()
                    }
                }
            }
            findClass("com.xs.fm.mine.impl.MineImpl").hook {
                injectMember {
                    method {
                        name = "getVipExpireTimeToDate"
                    }
                    afterHook {
                        result = "2599-12-31"
                    }
                }
            }
        }
        //App名称-七猫免费小说
        loadApp("com.kmxs.reader") {
            //hook-vip
            findClass("com.qimao.qmreader.reader.viewmodel.SingleVipViewModel").hook {
                injectMember {
                    method {
                        name = "v"
                        param(StringClass)
                    }
                    afterHook {
                        resultTrue()
                    }
                }
            }
            findClass("com.qimao.qmuser.model.entity.mine_v2.BaseInfo").hook {
                injectMember {
                    method {
                        name = "isVipState"
                    }
                    afterHook {
                        resultTrue()
                    }
                }
                injectMember {
                    method {
                        name = "isVipExpired"
                    }
                    afterHook {
                        result = "const-string v0, \\\"2\\\""
                    }
                }
                injectMember {
                    method {
                        name = "isShowYearVip"
                    }
                    afterHook {
                        resultTrue()
                    }
                }
            }
            findClass("com.baidu.mobads.sdk.api.SplashAd").hook {
                injectMember {
                    method {
                        name = "showWithBottomView"
                    }
                    afterHook {
                        resultTrue()
                    }
                }
            }


        }
        //App名称-淘小说
        loadApp("com.martian.ttbook") {
            //hook-vip
            findClass("com.martian.rpauth.response.MartianRPAccount").hook {
                injectMember {
                    method {
                        name = "getIsVip"
                    }
                    afterHook {
                        result = 1
                    }
                }
            }
        }
        //App-最右去广告
        loadApp("cn.xiaochuankeji.tieba") {

//            method 1
            "cn.xiaochuankeji.hermes.core.Hermes".hook {
                injectMember {
                    method {
                        name = "getProviderList\$core_release"
                    }
                    afterHook {
                        (result as MutableList<*>).clear()
                    }
                }.onAllFailure {
                    loggerE(msg = "Hook getProviderList fail: ${it.message}")
                }
            }

            "cn.xiaochuankeji.hermes.core.workflow.init.InitUtil".hook {
                injectMember {
                    method {
                        name = "init"
                        paramCount = 1
                    }
                    replaceToFalse()
                }.onAllFailure {
                    loggerE(msg = "Hook init fail: ${it.message}")
                }
            }

//            method 2
            "cn.xiaochuankeji.hermes.core.provider.ADProvider".hook {
                injectMember {
                    method {
                        name = "getChannel"
                    }
                    beforeHook {
                        result = 0
                    }
                }.onAllFailure {
                    loggerE(msg = "Hook getChannel fail: ${it.message}")
                }
                injectMember {
                    method {
                        name = "getConfigKey"
                    }
                    beforeHook {
                        result = ""
                    }
                }.onAllFailure {
                    loggerE(msg = "Hook getConfigKey fail: ${it.message}")
                }
            }

//            method 3
            "cn.xiaochuankeji.hermes.core.api.entity.ADSDKConfigResponseData".hook {
                injectMember {
                    method {
                        name = "getEnable"
                    }
                    replaceToFalse()
                }.onAllFailure {
                    loggerE(msg = "Hook getEnable fail: ${it.message}")
                }
            }

//            method 4
            "cn.xiaochuankeji.hermes.core.api.entity.ThirdSDKConfigResponse".hook {
                injectMember {
                    method {
                        name = "getEnable"
                    }
                    replaceToFalse()
                }.onAllFailure {
                    loggerE(msg = "Hook getEnable fail: ${it.message}")
                }
            }

//            method 5
            "cn.xiaochuankeji.hermes.core.api.entity.ADConfigResponseDataKt".hook {
                injectMember {
                    method {
                        name = "allRegisteredSDKConfigs"
                        paramCount = 1
                    }
                    beforeHook {
                        result = emptyMap<Any, Any>()
                    }
                }.onAllFailure {
                    loggerE(msg = "Hook allRegisteredSDKConfigs fail: ${it.message}")
                }
            }

        }
        //App名称-葫芦侠虚拟机
        loadApp("com.huluxia.vm") {
            //hook-vip
            findClass("com.huluxia.http.model.bean.UserLoginInfo").hook {
                injectMember {
                    method {
                        name = "getUserType"
                    }
                    afterHook {
                        result = 1
                    }
                }
                injectMember {
                    method {
                        name = "getExpireTime"
                    }
                    afterHook {
                        result = "2024-08-25"
                    }
                }
                injectMember {
                    method {
                        name = "getNickname"
                    }
                    afterHook {
                        result = "姜衍QQ:782045011"
                    }
                }
                injectMember {
                    method {
                        name = "isMember"
                    }
                    afterHook {
                        resultTrue()
                    }
                }
            }
        }
        //App名称-云记
        loadApp("com.jideos.jnotes") {
            //hook-vip
            findClass("com.jideos.base.http.Bean.Privilege").hook {
                injectMember {
                    method {
                        name = "getPrivilege"
                    }
                    afterHook {
                        result = 1
                    }
                }
            }
        }
        //App名称-Fake Location
        loadApp("com.lerist.fakelocation") {
            //hook-vip
            findClass("ށ.ރ.ؠ.ؠ.֏").hook {
                injectMember {
                    method {
                        name = "ޅ"
                    }
                    afterHook {
                        resultTrue()
                    }
                }
            }
        }
    }

    //原生XP-API方法
    override fun onXposedEvent() {
        YukiXposedEvent.events {
            onHandleLoadPackage {

                if (it.packageName == "com.dragon.read") {
                    val cls = XposedHelpers.findClass("com.dragon.read.pages.main.MainFragmentActivity", it.classLoader)
                    XposedHelpers.findAndHookMethod(cls, "onCreate", Bundle::class.java, object : XC_MethodHook() {
                        @Throws(Throwable::class)
                        override fun afterHookedMethod(param: MethodHookParam) {
                            val obj = cls.getField("i")[param.thisObject]
                            val cls2 = XposedHelpers.findClass("com.dragon.read.widget.b.a", it.classLoader)
                            val view = cls2.getMethod("getView").invoke(obj) as View
                            view.visibility = View.GONE
                        }
                    })
                }
//                模块2
                if (it.packageName == "com.lerist.fakelocation") {
                    XposedHelpers.findAndHookMethod("ށ.ރ.ؠ.ވ.֏\$֏",
                        it.classLoader,
                        "֏",
                        "ށ.ރ.ށ.ރ.ؠ.֏,java.lang.Object",
                        object : XC_MethodHook() {
                            @Throws(Throwable::class)
                            override fun beforeHookedMethod(param: MethodHookParam) {
                                param.setResult(java.lang.Boolean.valueOf("true"))
                            }
                        })
                }


            }
        }
    }
}